//
//  KSFilterCell.swift
//  SlaughterClient
//
//  Created by 邱仙凯 on 2021/9/17.
//  Copyright © 2021 aowang. All rights reserved.
//

import UIKit
 class KSFilterCell: UITableViewCell {
    
    private var itemLeftM:CGFloat = 10
     var multiItemClosure:((Int)->())? // 多选的 选中按钮 tag 返回
    var singleItemClosure:((Int?,Int)->())?  // 单选的选中按钮返回 0- 旧的选中tag，1- 新的选中tag
    var dateClosure:((Int)->())? // 1  开始  2 结束
    var textFClosure:((String)->())?
     var btnFieldClosure:(()->())?
    var minMoneyClosure:((String)->())? // 小金额返回
    var maxMoneyClosure:((String)->())? // 大金额返回
    
    private lazy var startBtn:SlideButton = {
        let lbtn = SlideButton()
        lbtn.layer.masksToBounds = true
        lbtn.layer.cornerRadius = 5
        lbtn.layer.borderWidth = 1
        lbtn.setTitle("请选择", for: .normal)
        lbtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        lbtn.layer.borderColor = UIColor.lightGray.cgColor
        return lbtn
    }()
    private lazy var line:UIView = {
        let line = UIView()
        line.backgroundColor = .lightGray
        return line
    }()
    private lazy var endBtn:SlideButton = {
        let rbtn = SlideButton()
        rbtn.layer.masksToBounds = true
        rbtn.layer.cornerRadius = 5
        rbtn.layer.borderWidth = 1
        rbtn.setTitle("请选择", for: .normal)
        rbtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        rbtn.layer.borderColor = UIColor.lightGray.cgColor
        return rbtn
    }()
    private lazy var inputTextF:UITextField = {
        let v: UITextField = UITextField()
//        v.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 6, height: 10))
        v.layer.masksToBounds = true
        v.textAlignment = .center
        v.layer.cornerRadius = 5
//        v.layer.borderWidth = 1
        v.font = UIFont.systemFont(ofSize: 15)
//        v.layer.borderColor = UIColor.lightGray.cgColor
        v.keyboardType = .numbersAndPunctuation
        v.placeholder = "请输入"
        v.backgroundColor = UIColor(kshex: "#E5E8EE")
        return v
    }()
     private lazy var btnField:UIButton = {
         let v: UIButton = UIButton()
         v.backgroundColor = UIColor(kshex: "#E5E8EE")
         v.layer.masksToBounds = true
         v.setTitleColor(.black, for: .normal)
         v.titleLabel?.textAlignment = .center
         v.layer.cornerRadius = 5
      
         v.titleLabel?.font = UIFont.systemFont(ofSize: 15)
       
         return v
     }()
    private lazy var textMin:UITextField = {
        let lbtn: UITextField = UITextField()
        lbtn.layer.masksToBounds = true
        lbtn.textAlignment = .center
        lbtn.layer.cornerRadius = 5
        lbtn.layer.borderWidth = 1
        lbtn.font = UIFont.systemFont(ofSize: 15)
        lbtn.layer.borderColor = UIColor.lightGray.cgColor
        lbtn.keyboardType = .numbersAndPunctuation
        lbtn.placeholder = "请输入"
        return lbtn
    }()
    private lazy var textMax:UITextField = {
        let lbtn: UITextField = UITextField()
        lbtn.layer.masksToBounds = true
        lbtn.textAlignment = .center
        lbtn.layer.cornerRadius = 5
        lbtn.layer.borderWidth = 1
        lbtn.font = UIFont.systemFont(ofSize: 15)
        lbtn.layer.borderColor = UIColor.lightGray.cgColor
        lbtn.placeholder = "请输入"
        return lbtn
    }()
    var itemBtnArr = [SlideItemButton]()
    private var singleSelectedBtn:SlideItemButton?
     func setborderColor(color:UIColor) {
         inputTextF.layer.borderColor = color.cgColor
         btnField.layer.borderColor = color.cgColor
         
         startBtn.layer.borderColor = color.cgColor
         endBtn.layer.borderColor = color.cgColor

     }
    var model:SlideContent? {
        didSet {
            if let model = model {
                startBtn.isHidden = true
                line.isHidden = true
                endBtn.isHidden = true
                inputTextF.isHidden = true
                btnField.isHidden = true
                textMin.isHidden = true
                textMax.isHidden = true
                
                if model.type == .dateRegion {
                    startBtn.isHidden = false
                    startBtn.setTitle(model.startDate, for: .normal)
                    startBtn.addTouchUpInSideAction { _ in
                        if let clo = self.dateClosure {
                            clo(1)
                        }
                        
                    }
                    if startBtn.superview.isNone {
                        self.contentView.addSubview(startBtn)
                        startBtn.snp.makeConstraints { (make) in
                            make.left.equalTo(self.contentView).offset(itemLeftM)
                            make.right.equalTo(self.contentView.snp.centerX).offset(-10)
                            make.top.equalTo(self.contentView).offset(6)
                            make.height.equalTo(35)
                            make.bottom.equalTo(self.contentView).offset(-6)
                        }
                    }
                    line.isHidden = false
                    if line.superview.isNone {
                        self.contentView.addSubview(line)
                        line.snp.makeConstraints { (make) in
                            make.centerY.equalTo(startBtn)
                            make.width.equalTo(5)
                            make.centerX.equalTo(self.contentView)
                            make.height.equalTo(1)
                        }
                    }
                    endBtn.isHidden = false
                    endBtn.setTitle(model.endDate, for: .normal)
                    endBtn.addTouchUpInSideAction { _ in
                        if let clo = self.dateClosure {
                            clo(2)
                        }
                    }
                    if endBtn.superview.isNone {
                        self.contentView.addSubview(endBtn)
                        endBtn.snp.makeConstraints { (make) in
                            make.right.equalTo(self.contentView).offset(-itemLeftM)
                            make.left.equalTo(self.contentView.snp.centerX).offset(10)
                            make.centerY.equalTo(startBtn)
                            make.height.equalTo(35)
                        }
                    }
                    
                    
                } else if model.type == .moneyRegion {
                    textMin.isHidden = false
                    textMin.text = model.minMoney
                    textMin.addTextChangeClosure { text in
                             if let clo = self.minMoneyClosure {
                                 clo(text.text.unwrap)
                            }
                        }
                    if textMin.superview.isNone {
                        self.contentView.addSubview(textMin)
                        textMin.snp.makeConstraints { (make) in
                            make.left.equalTo(self.contentView).offset(itemLeftM)
                            make.right.equalTo(self.contentView.snp.centerX).offset(-10)
                            make.top.equalTo(self.contentView).offset(6)
                            make.height.equalTo(35)
                            make.bottom.equalTo(self.contentView).offset(-6)
                        }
                    }
                    line.isHidden = false
                    if line.superview.isNone {
                        self.contentView.addSubview(line)
                        line.snp.makeConstraints { (make) in
                            make.centerY.equalTo(textMin)
                            make.width.equalTo(5)
                            make.centerX.equalTo(self.contentView)
                            make.height.equalTo(1)
                        }
                    }
                    textMax.isHidden = false
                    textMax.text = model.maxMoney
                    textMax.addTextChangeClosure { text in
                            if let clo = self.maxMoneyClosure {
                                clo(text.text.unwrap)
                            }
                        }
 
                    if textMax.superview.isNone {
                        self.contentView.addSubview(textMax)
                        textMax.snp.makeConstraints { (make) in
                            make.right.equalTo(self.contentView).offset(-itemLeftM)
                            make.left.equalTo(self.contentView.snp.centerX).offset(10)
                            make.centerY.equalTo(textMin)
                            make.height.equalTo(35)
                        }
                    }
                    
                } else if model.type == .multiSelect || model.type == .singleSelect {
                    
                    if itemBtnArr.count > 0 {
                        for i in 0..<itemBtnArr.count {
                            
                            itemBtnArr[i].setSelected(model.dataArr![i].ks_selected)
                        }
                    } else {
                        let padH = 10
                        let padV = 6
                        let itemH = 30
                        let rowx = model.numberOfRow
                        let contentVWidth = KSGlobal.screenW
                        let itemW = (Int(contentVWidth) - padH * (rowx + 1)) / rowx
                        for i in 0..<model.dataArr!.count {
                            let btn = SlideItemButton()
                            btn.tag = i
                            itemBtnArr.append(btn)
                            btn.setTitle(model.dataArr![i].title, for: .normal)
                            btn.setSelected( model.dataArr![i].ks_selected)
                            self.contentView.addSubview(btn)
                            btn.addTouchUpInSideAction {  [i] _ in
                                
                                if model.type == .singleSelect {
                                    if self.singleSelectedBtn.isSome {
                                        self.singleSelectedBtn?.setSelected(false)
                                        btn.setSelected(true)
                                    } else {
                                        btn.setSelected(true)
                                    }
                                    
                                    if let clo = self.singleItemClosure {
                                        clo(self.singleSelectedBtn?.tag,btn.tag)
                                    }
                                    self.singleSelectedBtn = btn
                                } else  {
                                     if let clo = self.multiItemClosure {
                                        clo(i)
                                    }
                                }
                                
                                
                                
                            }
                            btn.snp.makeConstraints { (make) in
                                make.width.equalTo(itemW)
                                make.height.equalTo(itemH)
                                make.left.equalTo(self.contentView).offset(padH + i%rowx * (itemW + padH))
                                make.top.equalTo(i/rowx * (itemH + padV))
                                if i == (model.dataArr!.count - 1) {
                                    make.bottom.equalTo(self.contentView).offset(-10)
                                }
                            }
                        }
                    }
                    
                    
                    
                } else if model.type == .singleInput {
                    inputTextF.isUserInteractionEnabled = !model.disabled
                
                    inputTextF.isHidden = false
                    inputTextF.text = model.inputStr
                    
                    inputTextF.addTextChangeClosure { text in
                            if let clo = self.textFClosure {
                                clo(text.text.unwrap)
                            }
                        }
                     if inputTextF.superview.isNone {
                        self.contentView.addSubview(inputTextF)
                         inputTextF.snp.makeConstraints { (make) in
                            make.left.equalTo(self.contentView).offset(itemLeftM)
                            make.right.equalTo(self.contentView).offset(-itemLeftM)
                            make.top.equalTo(self.contentView).offset(6)
                            make.bottom.equalTo(self.contentView).offset(-6)
                            make.height.equalTo(35)
                        }
                    }
                    
                    
                } else if model.type == .actionClo {
                    btnField.isHidden = false
                    btnField.setTitle(model.inputStr, for: .normal)
                    
                    btnField.addTouchUpInSideAction({ _ in
                        if let clo = self.btnFieldClosure {
                            clo()
                        }
                    })
                     if btnField.superview.isNone {
                        self.contentView.addSubview(btnField)
                         btnField.snp.makeConstraints { (make) in
                            make.left.equalTo(self.contentView).offset(itemLeftM)
                            make.right.equalTo(self.contentView).offset(-itemLeftM)
                            make.top.equalTo(self.contentView).offset(6)
                            make.bottom.equalTo(self.contentView).offset(-6)
                            make.height.equalTo(35)
                        }
                    }
                    
                    
                }
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
    }
    
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
